package likouJava;

import java.util.HashSet;
import java.util.Set;

public class 最长连续序列 {
    public int longestConsecutive(int[] nums) {
        Set<Integer> num_set = new HashSet<Integer>();
        for ( int num: nums){
            num_set.add(num);
        }
        int maxlength = 0;

        for(int num: num_set){
            if(!num_set.contains(num - 1)){
                //确保左面没了
                int currentNum = num;
                int currentStreak = 1;

                while (num_set.contains(currentNum + 1)){
                    //确保右边没了
                    currentNum += 1;
                    currentStreak += 1;
                }

                maxlength = Math.max(maxlength,currentStreak);
            }
        }
        return maxlength;
    }

}
